package com.mr_ladeng.listview;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

import javax.security.auth.PrivateCredentialPermission;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.mr_ladeng.listviewnest.R;
import com.mr_ladeng.utils.ImageCacheManager;
//import com.mr_ladeng.utils.ImageLoader;

/**
 * 
 * @项目名称：	Mr_ladeng_wish
 * @类名称： 	ChildAdapter.java
 * @创建人：	Mr.ladeng (zbl704@yeah.net)
 * @创建时间： 2015-4-22上午11:55:10
 * @修改备注：    
 * @version 1.0   
 * @类描述：	还愿之旅 	评论列表适配器	adapter
 */
public class PraentAdapter extends BaseAdapter {
	
	private Context mContext;
		
		Handler handler = null;
		private LayoutInflater inflater;
		private List<PullBeanRedeemParent> pullBeanRedeem;
		
		private String rea_words_time;
		private ImageLoader imageLoader;

		int count = 0 ;
		boolean isFirst = true ;

		ArrayList<HashMap<String, Object>> childList = new ArrayList<HashMap<String, Object>>();

		List<PullBeanRedeemChild> pullBeanRedeemChild = new ArrayList<PullBeanRedeemChild>();

		Calendar c = Calendar.getInstance();

		private ImageLoader imageLoader2;

		public PraentAdapter(Context context,List<PullBeanRedeemParent> pullBeanRedeem) {

			inflater = LayoutInflater.from(context);

			imageLoader = new ImageLoader(Volley.newRequestQueue(context),ImageCacheManager.getInstance());
			
//			imageLoader2 = new ImageLoader(context);  
			

			this.pullBeanRedeem = pullBeanRedeem;
			this.mContext = context;
		}
		

		@Override
		public int getCount() {
			return pullBeanRedeem.size();
		}

		@Override
		public Object getItem(int position) {
			return pullBeanRedeem.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView( int position, View convertView, ViewGroup parent) {
			
			final int show = 1 ;
			
			ViewHoder viewHoder = null;
			
			if (convertView == null) {

				viewHoder = new ViewHoder();

				convertView = inflater.inflate(R.layout.parentitem, null, false);
				
				//日期
				viewHoder.redeemData = (TextView) convertView.findViewById(R.id.redeem_data);
				//左边时间
				viewHoder.parentShowTime = (TextView) convertView.findViewById(R.id.parent_show_time);
				//日期条目布局（用于判断今日内容，如果今天内容>1则隐藏该条目）
				viewHoder.redeemDataLl = (LinearLayout) convertView.findViewById(R.id.redeem_data_ll);

				// 还愿人图像（左边）
				viewHoder.redeemManImg = (RoundImageView) convertView.findViewById(R.id.redeem_men_img);
				
				// 我的图像（右边）
				viewHoder.redeemMyImg = (RoundImageView) convertView.findViewById(R.id.redeem_my_img);
//				viewHoder.redeemMyImg.setVisibility(View.INVISIBLE);
				// 内容
				viewHoder.redeemContent = (TextView) convertView.findViewById(R.id.redeem_content);

				// 评论和赞
				viewHoder.redeemReply = (LinearLayout) convertView.findViewById(R.id.redeem_reply);
				viewHoder.redeemReply.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
//						Toast.makeText(con, "暂不能评论", Toast.LENGTH_SHORT).show();
					}
				});
				viewHoder.redeemPraise = (LinearLayout) convertView.findViewById(R.id.redeem_praise);
				viewHoder.redeemPraise.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
//						Toast.makeText(RedeemTravelActivity.this, "点赞达人", Toast.LENGTH_SHORT).show();
					}
				});
				
				//赞数
				viewHoder.redeemPraiseNum = (TextView) convertView.findViewById(R.id.redeem_praise_num);
				
				// 评论数
				viewHoder.redeemReplyNum = (TextView) convertView.findViewById(R.id.redeem_reply_num);

				//评论listView
				final ChildLiistView redeemReplyListview = (ChildLiistView) convertView.findViewById(R.id.redeem_reply_listview);
				final ImageView replyTopline = (ImageView) convertView.findViewById(R.id.reply_top);
				
				
				pullBeanRedeemChild = new ArrayList<PullBeanRedeemChild>();
				
				final ChildAdapter childAdapter = new ChildAdapter(mContext);
//				childAdapter.addAll(pullBeanRedeemChild);
				childAdapter.addAll(childList);
				redeemReplyListview.setAdapter(childAdapter);
				
				
				// 显示评论
				viewHoder.redeemTvReply = (TextView) convertView.findViewById(R.id.redeem_tv_reply);
				
				viewHoder.redeemTvReply.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (redeemReplyListview.getVisibility()==View.VISIBLE)  {
					       redeemReplyListview.setVisibility(View.GONE);
					       replyTopline.setVisibility(View.GONE);
//					       childAdapter.clearAll();   
						}else {
						redeemReplyListview.setVisibility(View.VISIBLE);
						replyTopline.setVisibility(View.VISIBLE);
						childAdapter.addAll(childList);
						childAdapter.notifyDataSetChanged();
						}
					}
				});
				
				viewHoder.redeem_showall = (TextView) convertView.findViewById(R.id.redeem_showall);
				viewHoder.redeem_showall.setOnClickListener(new ShowAll(viewHoder));
				//TODO
				new myAsyncTask(viewHoder).execute();
				
				//发表的图片
				viewHoder.redeem_pic_layout1 = (LinearLayout)convertView.findViewById(R.id.redeem_pic_layout1);
				viewHoder.redeem_pic_layout2 = (LinearLayout)convertView.findViewById(R.id.redeem_pic_layout2);
				viewHoder.redeem_pic_layout3 = (LinearLayout)convertView.findViewById(R.id.redeem_pic_layout3);
				viewHoder.redeem_pic_layout = (LinearLayout)convertView.findViewById(R.id.redeem_pic_layout);
				//NetworkImageView
				viewHoder.p1 = (NetworkImageView)convertView.findViewById(R.id.p1);
				viewHoder.p11 = (NetworkImageView)convertView.findViewById(R.id.p11);
				viewHoder.p2 = (NetworkImageView)convertView.findViewById(R.id.p2);
				viewHoder.p3 = (NetworkImageView)convertView.findViewById(R.id.p3);
				viewHoder.p4 = (NetworkImageView)convertView.findViewById(R.id.p4);
				viewHoder.p5 = (NetworkImageView)convertView.findViewById(R.id.p5);
				viewHoder.p6 = (NetworkImageView)convertView.findViewById(R.id.p6);
				viewHoder.p7 = (NetworkImageView)convertView.findViewById(R.id.p7);
				viewHoder.p8 = (NetworkImageView)convertView.findViewById(R.id.p8);
				viewHoder.p9 = (NetworkImageView)convertView.findViewById(R.id.p9);
				
				
				convertView.setTag(viewHoder);

			} else {
				viewHoder = (ViewHoder) convertView.getTag();
			}
			
			

			childList = new ArrayList<HashMap<String, Object>>();
			for (int i = 0; i < 15; i++) {
				HashMap<String, Object> ParentMap = new HashMap<String, Object>();
				ParentMap.put("nickname_1", "阿辉");
				ParentMap.put("nickname_2", "阿宝");
				ParentMap.put("rea_message_content", "喀喀喀卡卡卡啊");
				ParentMap.put("mark", "");
				String mark = PullBeanRedeemChild.getMark();
				childList.add(ParentMap);
			}
				
			
			
			
			PullBeanRedeemParent pullget = pullBeanRedeem.get(position);
			//获取当前时间
			StringTokenizer nowTimedata = new StringTokenizer(DateUtils.getDateToString(System.currentTimeMillis()/1000), " ");  
			 String nowTime = "" ;
			while (nowTimedata.hasMoreTokens()) {
				nowTime = nowTimedata.nextToken();
			   break;
			}
			//截取数据中的时间
			StringTokenizer token = new StringTokenizer(DateUtils.getDateToString(Long.parseLong(pullget.rea_words_time)), " ");  
			 String dataData = "" ;
			 String dataTimer = "" ;
			while (token.hasMoreTokens()) {
				dataData = token.nextToken();
				dataTimer = token.nextToken();
			}
			
			//比对时间
				
			count++;
			try {
			 StringTokenizer thisTime2 = new StringTokenizer(DateUtils.getDateToString(Long.parseLong(pullBeanRedeem.get(count).getRea_words_time())), " ");
			 StringTokenizer nextTime2 = new StringTokenizer(DateUtils.getDateToString(Long.parseLong(pullBeanRedeem.get(count+1).getRea_words_time())), " ");
			 String thisTime = "" ;
			 String nextTime = "" ;
			while (thisTime2.hasMoreTokens()) {
				thisTime = thisTime2.nextToken();
				break;
			}
			while (nextTime2.hasMoreTokens()) {
				nextTime = nextTime2.nextToken();
				break;
			}
					//判断系统时间和数据时间
				if (nowTime.equals(dataData)) {
					if (pullBeanRedeem.get(position-1).equals(0)==true) {
						viewHoder.redeemData.setText("今天");
						viewHoder.redeemDataLl.setVisibility(View.VISIBLE);
					}else if(!pullBeanRedeem.get(position-1).equals(0)==true){
						viewHoder.redeemDataLl.setVisibility(View.GONE);
					}
				} else {
					if (pullBeanRedeem.get(position-1).equals(0)==true) {
						viewHoder.redeemData.setText(dataData);
						viewHoder.redeemDataLl.setVisibility(View.VISIBLE);
					}else {
						viewHoder.redeemDataLl.setVisibility(View.GONE);
					}
				// viewHoder.redeemData.setText(dataData);
				// 判断这条item和下一条item的时间是否一样
				// if (thisTime.equals(nextTime)) {
				// viewHoder.redeemDataLl.setVisibility(View.GONE);
				// }
				// else {
				// viewHoder.redeemDataLl.setVisibility(View.VISIBLE);
				//		}
			}

			} catch (Exception e) {
			}
			
			 viewHoder.redeemManImg.setTag(pullget.headpic);
			 viewHoder.redeemContent.setText(pullget.rea_words_content);
			 viewHoder.redeemReplyNum.setText(pullget.reply_num);
			 viewHoder.parentShowTime.setText(dataTimer);
//			 LoadImage.getImage(viewHoder.redeemManImg, pullget.headpic);
			 
//			 imageLoader2.DisplayImage(pullget.headpic, viewHoder.redeemManImg);  

			 try {
				 int a = Integer.parseInt(pullget.praise.toString().trim());
				 System.out.println(a);
				 if (a>1000) {
					 viewHoder.redeemPraiseNum.setText("999+");
				 }else {
					 viewHoder.redeemPraiseNum.setText(pullget.praise);
				 }
			} catch (Exception e) {
			}
			 
			
			 
			 //	 viewHoder.redeemMyImg.setTag(pullget.p1);
			 //	 LoadImage.getImage(viewHoder.redeemMyImg, pullget.p1);

			 viewHoder.p1.setTag(pullget.p1);
			 viewHoder.p11.setTag(pullget.p1);
			 viewHoder.p1.setTag(pullget.p2);
			 viewHoder.p1.setTag(pullget.p3);
			 viewHoder.p1.setTag(pullget.p4);
			 viewHoder.p1.setTag(pullget.p5);
			 viewHoder.p1.setTag(pullget.p6);
			 viewHoder.p1.setTag(pullget.p7);
			 viewHoder.p1.setTag(pullget.p8);
			 viewHoder.p1.setTag(pullget.p9);
			 

				viewHoder.redeem_pic_layout2.setVisibility(View.GONE);
				viewHoder.redeem_pic_layout3.setVisibility(View.GONE);
			
				
				if (pullget.p1 == null|| pullget.p1.equals("")) {
					viewHoder.redeem_pic_layout1.setVisibility(View.GONE);
					viewHoder.redeem_pic_layout2.setVisibility(View.GONE);
					viewHoder.redeem_pic_layout3.setVisibility(View.GONE);
					viewHoder.redeem_pic_layout.setVisibility(View.GONE);
					
				} else{
					
				if (pullget.p1 != null&& !pullget.p1.equals("")&&pullget.p2==null||pullget.p2.equals("")) {
						viewHoder.redeem_pic_layout.setVisibility(View.VISIBLE);
						viewHoder.redeem_pic_layout1.setVisibility(View.GONE);
						viewHoder.redeem_pic_layout2.setVisibility(View.GONE);
						viewHoder.redeem_pic_layout3.setVisibility(View.GONE);
						viewHoder.p11.setVisibility(View.VISIBLE);
						viewHoder.p11.setDefaultImageResId(R.drawable.load_bg2);
						viewHoder.p11.setImageUrl(pullget.p1, imageLoader);
						

//						 imageLoader2.DisplayImage(pullget.p1, viewHoder.p11); 
						
					} 
					
				if (pullget.p2!=null&& !pullget.p2.equals("")) { 
					viewHoder.redeem_pic_layout1.setVisibility(View.VISIBLE);
					viewHoder.p11.setVisibility(View.GONE);
					viewHoder.p1.setVisibility(View.VISIBLE);
					viewHoder.p2.setVisibility(View.VISIBLE);
					viewHoder.p2.setDefaultImageResId(R.drawable.load_bg2);
					viewHoder.p2.setImageUrl(pullget.p2, imageLoader);
					viewHoder.p1.setDefaultImageResId(R.drawable.load_bg2);
					viewHoder.p1.setImageUrl(pullget.p1, imageLoader);

//					 imageLoader2.DisplayImage(pullget.p2, viewHoder.p2); 
//					 imageLoader2.DisplayImage(pullget.p1, viewHoder.p1); 
					
				} 
				
				if (pullget.p3 == null|| pullget.p3.equals("")) {
					viewHoder.p3.setVisibility(View.INVISIBLE);
					viewHoder.redeem_pic_layout2.setVisibility(View.GONE);
					viewHoder.redeem_pic_layout3.setVisibility(View.GONE);
					
				} 
			
				if (pullget.p3 != null&& !pullget.p3.equals("")) {
					viewHoder.p11.setVisibility(View.GONE);
					viewHoder.p3.setVisibility(View.VISIBLE);
					viewHoder.p1.setVisibility(View.VISIBLE);
					viewHoder.p2.setVisibility(View.VISIBLE);
					viewHoder.p3.setDefaultImageResId(R.drawable.load_bg2);
					viewHoder.p3.setImageUrl(pullget.p3, imageLoader);
					

//					 imageLoader2.DisplayImage(pullget.p3, viewHoder.p3); 
				} 
			

				if (pullget.p4 == null|| pullget.p4.equals("")) {
					viewHoder.redeem_pic_layout2.setVisibility(View.GONE);
					viewHoder.redeem_pic_layout3.setVisibility(View.GONE);
					
				}
				
				if (pullget.p4 != null&& !pullget.p4.equals("")){
					viewHoder.redeem_pic_layout2.setVisibility(View.VISIBLE);
					viewHoder.redeem_pic_layout3.setVisibility(View.GONE);
					viewHoder.p4.setVisibility(View.VISIBLE);
					viewHoder.p4.setDefaultImageResId(R.drawable.load_bg2);
					viewHoder.p4.setImageUrl(pullget.p4, imageLoader);

//					 imageLoader2.DisplayImage(pullget.p4, viewHoder.p4); 
				}
				if (pullget.p5 == null|| pullget.p5.equals("")) {
					viewHoder.redeem_pic_layout3.setVisibility(View.GONE);
					
				}
				if (pullget.p5 != null&& !pullget.p5.equals("")){
					viewHoder.p5.setVisibility(View.VISIBLE);
					viewHoder.p5.setDefaultImageResId(R.drawable.load_bg2);
					viewHoder.p5.setImageUrl(pullget.p5, imageLoader);

//					 imageLoader2.DisplayImage(pullget.p5, viewHoder.p5); 
				} 
				
				if (pullget.p6 != null&& !pullget.p6.equals("")){
					viewHoder.p6.setVisibility(View.VISIBLE);
					viewHoder.p6.setDefaultImageResId(R.drawable.load_bg2);
					viewHoder.p6.setImageUrl(pullget.p6, imageLoader);

//					 imageLoader2.DisplayImage(pullget.p6, viewHoder.p6); 
				}
				if (pullget.p7 == null|| pullget.p7.equals("")){
					viewHoder.redeem_pic_layout3.setVisibility(View.GONE);
					
				}
				if (pullget.p7 != null&& !pullget.p7.equals("")){
					viewHoder.redeem_pic_layout3.setVisibility(View.VISIBLE);
					viewHoder.p7.setDefaultImageResId(R.drawable.load_bg2);
					viewHoder.p7.setImageUrl(pullget.p7, imageLoader);

//					 imageLoader2.DisplayImage(pullget.p7, viewHoder.p7); 
				}
				if (pullget.p8 != null&& !pullget.p8.equals("")){
					viewHoder.p8.setDefaultImageResId(R.drawable.load_bg2);
					viewHoder.p8.setImageUrl(pullget.p8, imageLoader);

//					 imageLoader2.DisplayImage(pullget.p8, viewHoder.p8); 
				}
				if (pullget.p9 != null&& !pullget.p9.equals("")){
					viewHoder.p9.setDefaultImageResId(R.drawable.load_bg2);
					viewHoder.p9.setImageUrl(pullget.p9, imageLoader);

//					 imageLoader2.DisplayImage(pullget.p9, viewHoder.p9); 
				}
				}			
					
				
			return convertView;
		}
		
		class ViewHoder{
			
			TextView 			redeemContent, redeemPraiseNum , redeemTvReply , redeemReplyNum , redeemData , parentShowTime , redeem_showall;
			RoundImageView 		redeemManImg, redeemMyImg;
			ChildLiistView 		redeemReplyListview;
			LinearLayout 		redeemReply, redeemPraise , redeemDataLl ;
			LinearLayout 		redeem_pic_layout1 , redeem_pic_layout2 , redeem_pic_layout3 , redeem_pic_layout;
			NetworkImageView 	p1 , p11 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 ;
			ImageView replyTopline ;
		}
		
		private  class ShowAll implements OnClickListener{
			private ViewHoder viewHoder ;
			
			public ShowAll(ViewHoder viewHoder){
				this.viewHoder = viewHoder;
			}
			@Override
			public void onClick(View v) {
				if (viewHoder.redeem_showall.getText().equals("显示全部")) {
					viewHoder.redeemContent.setMaxLines(Integer.MAX_VALUE); 
					viewHoder.redeem_showall.setText("收起");
				}else {
					viewHoder.redeemContent.setMaxLines(5); 
					viewHoder.redeem_showall.setText("显示全部");
				}
			}
		}
		
	private class myAsyncTask extends AsyncTask<Void, Void, Void> {
		private ViewHoder viewHoder;
		public myAsyncTask(ViewHoder viewHoder) {
			this.viewHoder = viewHoder;
		}
		@Override
		protected void onPostExecute(final Void result  ) {
			super.onPostExecute(result);
			//TODO
			int lineCount = viewHoder.redeemContent.getLineCount();
			if (viewHoder.redeemContent.getLineCount()>4) {
				viewHoder.redeem_showall.setVisibility(View.VISIBLE);
			}else {
				viewHoder.redeem_showall.setVisibility(View.GONE);
			}
 
		}

		@Override
		protected Void doInBackground(Void... params) {
			return null;
		}
	}

	}